﻿using System;
using System.Collections.Generic;
using Android.Content;
using Android.Views;
using Android.Widget;
using Koush;
using Cinemacenter.Shared;

namespace Cinemacenter.Droid
{
	class MovieAdapter : BaseAdapter
	{
		readonly Context context;
		ViewHolderMovieInfo holderInfo;
		public IReadOnlyList<Movie> Movies;

		public override int Count {
			get { return Movies == null ? 0 : Movies.Count; }
		}

		public override Java.Lang.Object GetItem (int position)
		{
			return new Java.Lang.String (Movies [position].ToString ());
		}

		public MovieAdapter (Context context)
		{
			this.context = context;
		}

		public override long GetItemId (int position)
		{
			return Movies [position].GetHashCode ();
		}

		public override View GetView (int position, View convertView, ViewGroup parent)
		{
			if (convertView == null) {
				var inflater = LayoutInflater.FromContext (context);
				convertView = inflater.Inflate (Resource.Layout.ViewMovieItem, parent, false);
				holderInfo = new ViewHolderMovieInfo ();
				convertView.SetTag (Resource.Id.itemHolder, holderInfo);
				holderInfo.Poster = convertView.FindViewById<ImageView> (Resource.Id.posterView);
				holderInfo.Title = convertView.FindViewById<TextView> (Resource.Id.tvTitle);
				holderInfo.MovieClass = convertView.FindViewById<TextView> (Resource.Id.tvClass);
				holderInfo.Genre = convertView.FindViewById<TextView> (Resource.Id.tvGenre);
				holderInfo.Director = convertView.FindViewById<TextView> (Resource.Id.tvDirector);
				holderInfo.Protagonists = convertView.FindViewById<TextView> (Resource.Id.tvProtagonists);
			} else {
				holderInfo = (ViewHolderMovieInfo)convertView.GetTag (Resource.Id.itemHolder);
			}
		
			var item = Movies [position];
			UrlImageViewHelper.SetUrlDrawable (holderInfo.Poster, item.PosterUrl, Resource.Drawable.ic_fallback);
			holderInfo.Title.Text = item.Name;

			if (!String.IsNullOrEmpty (item.MovieClass)) {
				holderInfo.MovieClass.Text = item.MovieClass;
				holderInfo.MovieClass.Visibility = ViewStates.Visible;
			} else {
				holderInfo.MovieClass.Visibility = ViewStates.Gone;
			}
			if (!String.IsNullOrEmpty (item.Genre)) {
				holderInfo.Genre.Text = context.Resources.GetString (Resource.String.genre)
				+ ": " + item.Genre;
				holderInfo.Genre.Visibility = ViewStates.Visible;
			} else {
				holderInfo.Genre.Visibility = ViewStates.Gone;
			}

			if (!String.IsNullOrEmpty (item.Director)) {
				holderInfo.Director.Text = context.Resources.GetString (Resource.String.director)
				+ ": " + item.Director;
				holderInfo.Director.Visibility = ViewStates.Visible;
			} else {
				holderInfo.Director.Visibility = ViewStates.Gone;
			}

			if (!String.IsNullOrEmpty (item.Protagonists)) {
				holderInfo.Protagonists.Text = context.Resources.GetString (
					Resource.String.protagonists)
				+ ": " + item.Protagonists;
				holderInfo.Protagonists.Visibility = ViewStates.Visible;
			} else {
				holderInfo.Protagonists.Visibility = ViewStates.Gone;
			}

			return convertView;
		}
	}

	public class ViewHolderMovieInfo : Java.Lang.Object
	{
		public ImageView Poster;
		public TextView Title;
		public TextView MovieClass;
		public TextView Genre;
		public TextView Director;
		public TextView Protagonists;
	}
}
